package com.android.ailaoshan.location;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import com.android.ailaoshan.R;
import com.android.ailaoshan.common.MyNotification;

public class LocationService extends Service {

    private long infoNo;
    private LocationManager locationManager;
    private Intent borderCast;
    private MyLocationListener myLocationListener;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        // 显示通知
        final int NOTIFICATION_ID = 211;
        final String CHANEL_ID = "START_RECORD_LOCATION";

        MyNotification myNotification = new MyNotification();
        myNotification.createNotificationChanel(CHANEL_ID);
        Notification notification = myNotification.showNotification(R.string.notification_status2,
                R.string.notification_ticker2, CHANEL_ID);

        startForeground(NOTIFICATION_ID, notification);

        infoNo = System.currentTimeMillis();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        borderCast = new Intent("android.ailaoshan.locationChangeReceiver");
        myLocationListener = new MyLocationListener();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                30, 100, myLocationListener);
        return START_NOT_STICKY;
    }

    /**
     * 监听位置信息
     */
    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();

            // 保存位置
            saveLocation(lat, lng);

            // 发送广播
            borderCast.putExtra("lat", lat);
            borderCast.putExtra("lng", lng);
            borderCast.putExtra("infoNo", infoNo);
            sendOrderedBroadcast(borderCast, null);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onProviderDisabled(String provider) {}
    }

    /**
     * 存储位置信息
     */
    private void saveLocation(double lat,double lng){
        LocationInfo locationInfo = new LocationInfo();

        locationInfo.setLat(lat);
        locationInfo.setLng(lng);
        locationInfo.setTime(System.currentTimeMillis());
        locationInfo.setInfoNo(infoNo);
        locationInfo.setIsUpload(2);

        locationInfo.save();
    }

    @Override
    public void onDestroy(){
        if(locationManager != null){
            locationManager.removeUpdates(myLocationListener);
        }
    }
}
